/**
 * 设置驱动默认同步属性，默认根据字段相似名称映射，数据源同步属性只有一组，而目标源同步属性有多组
 * driver.sourceMapping.fileds : [{name=ID, type=4}, {name=NAME, type=12}]
 * driver.mappings.fileds : { [{name=ID, type=4}, {name=NAME, type=12}],[{name=ID, type=4}, {name=NAME, type=12}] }
 */
function setSyncDefProperty(driver){
	var mappings = driver.mappings;
	var mLen = mappings.length;
	for (var i = 0; i < mLen; i++) {
		setSyncSourceAndTarget(driver.sourceMapping, mappings[i]);
	}
}

/**
 * 设置目标源和数据源同步属性
 * @param sourceMapping 数据源
 * @param targetMapping 目标源
 * @returns
 */
function setSyncSourceAndTarget(sourceMapping, targetMapping){
	var sourceArr = sourceMapping.config.column;
	var targetArr = targetMapping.config.column;
	var sLen = sourceArr.length;
	var tLen = targetArr.length;
	
	//驱动列
	var sourceCols = [];
	var targetCols = [];
	var sLen = sourceArr.length;
	var tLen = targetArr.length;
	for (var i = 0; i < sLen; i++) {
		var sCol = sourceArr[i];
		var sName = sCol.name;
		var tCol = null;
		for (var k = 0; k < tLen; k++) {
			var tFiled = targetArr[k];
			var tName = tFiled.name;
			if(sName==tName){
				tCol = tFiled;
				break;
			}
		}
		if(tCol != null){
			sourceCols.push(sCol);
			targetCols.push(tFiled);
		}
	}
	//赋值
	sourceMapping.fileds = sourceCols;
	targetMapping.fileds = targetCols;
}

function check(){
	var driver = {
		"name": null,
		"model": "all",
		"enable": true,
		"sourceMapping": {},
		"mappings":[],
		"query": {
			"and":[],
			"or":[]
		},
		"policy": {}
	};
	
	//1.检查数据源配置
	var sourceConfig = validataDriverConfig($("#sourceConfig"));
	if(!sourceConfig){
		return;
	}
	driver.sourceMapping.connector = sourceConfig.connector;
	driver.sourceMapping.config = sourceConfig;
	driver.sourceMapping.fileds = [];
	// 去掉重复的属性
	delete sourceConfig.connector;

	//2.检查目标源配置
	var targetConfig = validataDriverConfig($("#targetConfig"));
	if(!targetConfig){
		return;
	}
	//组装目标源集合
	var targetMapping = {};
	targetMapping.connector = targetConfig.connector;
	targetMapping.config = targetConfig;
	targetMapping.fileds = [];
	delete targetConfig.connector;
	driver.mappings.push(targetMapping);
	
	//3.设置驱动默认名称
	var sourceConfigName = sourceConfig.name.toLocaleUpperCase();
	var targetConfigName = targetConfig.name.toLocaleUpperCase();
	driver.name = sourceConfigName+"_"+targetConfigName;
	
	//4.设置驱动默认同步属性，默认根据字段相似名称映射
	setSyncDefProperty(driver);
	
	//5.保存驱动配置
	$.loadingT(true);
	$.post($basePath + "/driver/add", {"mapping":JSON.stringify(driver)}, function(data) {
		$.loadingT(false);
		bootGrowl(data.resultValue,"success"); 
        if(data.success==true){
			$initContainer.load($basePath+'/driver/page/driverEdit?dirverName='+driver.name);
		}
	});
}

//切换连接器
function changeConnectorType($this){
	//连接器类型
	var connType = $this.val();
	//获取连接器配置元素
	var connectorConfig = $this.parent().parent().parent().find(".connectorConfig");
	//清空配置
	connectorConfig.html("");
	
	//从公共配置文件中读取配置的驱动页面地址
	for (var key in DriverConnectorConstant) {   
		if(connType==key){
			var val = DriverConnectorConstant[key];
			//加载页面
			connectorConfig.load($basePath+val['url']);
			break;
		}
	}
}

$(function() {
	//连接器切换事件
	$("select[name='connector']").change(function(){
		changeConnectorType($(this));
	});
	
	//保存
	$("#driverSubBtn").click(function(){
		check();
	});
	
	//返回
	$("#driverBackIndexBtn").click(function(){
		//加载页面
		$initContainer.load($basePath+"/index");
	});
	
})